<%@ page pageEncoding="UTF-8"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.base.DatabaseObject"%>
<%@page import="com.base.Config"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="com.wap.DBContentDAO"%>
<%@include file="inc-header.jsp"%><!-- MAIN CONTENT -->
<div class="box bottom10">
	<%@include file="inc-nav-header.jsp"%>
	<%
		String sCatId = Utils.getParam(request, "catid", "0");
		String sPage = Utils.getParam(request, "page", "1");
		String sId = Utils.getParam(request, "id", "0");
		String strAction = Utils.getParam(request, "action", "0");
		DatabaseObject gameCategory = contentDAO
				.getCategory(new BigDecimal(sCatId));
        System.out.println(sId);
		int icheckCount = contentDAO.checkCount(new BigDecimal(sId));
		int iCountDownload = contentDAO.viewDownloadCount(new BigDecimal(
				sId));

		if (icheckCount <= 0) {
			boolean b = contentDAO.insertContentDown(sId);
		} else {
			iCountDownload = contentDAO.viewDownloadCount(new BigDecimal(
					sId));
			boolean b = contentDAO.updateContentDownload(sId,
					iCountDownload);
		}
	%>
	<div class="tab2">
		<%@include file="dichvu-adv.jsp"%>
		>>
		<a href="game.jsp?act=dv"
			<%=gameCategory != null ? "" : "class=\"active\""%>> Game </a>
		<%
			if (gameCategory != null) {
		%>
		>>
		<a
			href="game.jsp?act=dv&catid=<%=gameCategory.getAttribute().get("ID")%>"
			class="active"> <%=gameCategory.getAttribute().get("DESCRIPTION")%></a>
		<%
			}
		%>
	</div>
	<!--end .tab2-->
	<!-- BODY CONTENT -->
	<div class="path bottom10">
		<a href="game.jsp?act=dv&type=new"
			<%="new".equalsIgnoreCase(sType) ? " class=\"active\""
					: ""%>> Mới
			nhất </a> |
		<a href="game.jsp?act=dv&type=hot"
			<%="hot".equalsIgnoreCase(sType) ? " class=\"active\""
					: ""%>> Hot
			nhất </a>
	</div>
	<div>
		<%
			int currPage = Integer.parseInt(sPage);
			int rowsPerPage = 5;
			if (!StringTool.isEmptyOrNul(sCatId)) {
				int totalRecs = contentDAO
						.countContentByCategory(new BigDecimal(sCatId));
				int numOfPages = (int) ((totalRecs % rowsPerPage != 0) ? (totalRecs
						/ rowsPerPage + 1)
						: (totalRecs / rowsPerPage));
				if (totalRecs <= rowsPerPage)
					numOfPages = 1;
				if (totalRecs == 0)
					numOfPages = 0;
				if (currPage > numOfPages)
					currPage = numOfPages;
				if (currPage <= 0)
					currPage = 1;
				long startPos = rowsPerPage * (currPage - 1) + 1;
				long stopPos = startPos + rowsPerPage - 1;
				if (stopPos > totalRecs)
					stopPos = totalRecs;
				long firstPage = 1; // The first page
				long lastPage = numOfPages; // The last page
				long prevPage = currPage - 1;
				if (prevPage < 1) {
					prevPage = 1;
				}
				long nextPage = currPage + 1;
				if (nextPage > numOfPages) {
					nextPage = numOfPages;
				}
				ArrayList<DatabaseObject> gameList = contentDAO
						.getContentListByCategory(new BigDecimal(sCatId),
								currPage, rowsPerPage);

				if (gameList == null || gameList.isEmpty()) {
					if ("new".equalsIgnoreCase(sType)) {
						gameList = contentDAO.getContentListByType(
								new BigDecimal("1065"), 1, 5);
					} else if ("hot".equalsIgnoreCase(sType)) {
						gameList = contentDAO.getContentHOTListByType(
								new BigDecimal("1065"), 1, 5);
					}
				}
				if (gameList != null && !gameList.isEmpty()) {
		%>
		<table cellpadding="0" cellspacing="0" border="1" width="100%">
			<%
				for (DatabaseObject game : gameList) {
			%>
			<tr>
				<td width="4%" style="padding: 2px;">
					<a
						href="game-detail.jsp?act=dv&catid=<%=game.getAttribute().get("category_id")%>&id=<%=game.getAttribute().get("id")%>&page=<%=currPage%>"><img
							src="<%=imagePath
								+ game.getAttribute().get("avatar_image")%>"
							alt="<%=game.getAttribute().get("name")%>" class="img-45-30">
					</a>
				</td>
				<td width="96%"
					style="vertical-align: top; padding: 5px 2px 5px 4px;">
					<input type="hidden" name="action" value="" />
					<b><a
						href="game-detail.jsp?act=dv&catid=<%=game.getAttribute().get("category_id")%>&id=<%=game.getAttribute().get("id")%>&page=<%=currPage%>"><strong>
								<%=game.getAttribute().get("name")%> </strong> </a> </b>
					<%
						dichden = getSevicesCode(Integer.parseInt(game
											.getAttribute().get("money").toString()));

									String ua = request.getHeader("User-Agent")
											.toLowerCase();
									if (ua != null) {
										ua = ua.toUpperCase();
									}
									if (ua.indexOf("NOKIA") >= 0
											|| ua.indexOf("SAMSUNG") >= 0
											|| ua.indexOf("MOTOROLA") >= 0
											|| ua.indexOf("LG") >= 0
											|| ua.indexOf("HTC") >= 0
											|| ua.indexOf("GALAXY") >= 0
											|| ua.indexOf("BLACKBERRY") >= 0
											|| ua.indexOf("SONY") >= 0
											|| ua.indexOf("APPLE") >= 0) {
					%>
					<form>
						<input type ="hidden" name=id value=<%=game.getAttribute().get("id")%>>
						Để
						<a	href="sms:<%=dichden%>?body=<%=game.getAttribute().get("code")%>"><input type=submit value="Tải về">
						</a> soạn tin:
						<b class="show"><%=game.getAttribute().get("code")%></b> gửi
						<b class="show"><%=dichden%></b>
					</form>

					<%
						} else {
					%>
					<p>
						Để tải về soạn tin:
						<b class="show"><%=game.getAttribute().get("code")%></b> gửi
						<b class="show"><%=dichden%></b>
					</p>
					<%
						}
					%>
				</td>
			</tr>
			<%
				}
			%>
		</table>
		<%
			}
		%>
		<div align="center">
			<%
				long startPage = currPage - 3;
					if (startPage < 1)
						startPage = 1;

					long endPage = startPage + 6;
					if (endPage > numOfPages) {
						endPage = numOfPages;
					}
					if (startPage > 3 && endPage - startPage < 6)
						startPage = endPage - 6;
					if (startPage > 1) {
			%>
			<a href="?act=dv&catid=<%=sCatId%>&page=1">Đầu</a>
			<a href="?act=dv&catid=<%=sCatId%>&page=<%=startPage - 1%>">
				Trước </a>
			<%
				}
					for (long i = startPage; i <= endPage; i++) {
						if (currPage == i) {
							out
									.print("<font style=\"font-weight:bold; font-size: 12px; color: #ff0000;\">"
											+ i + "</font>");
						} else {
			%>
			<a href="?act=dv&catid=<%=sCatId%>&page=<%=i%>"
				style="font-size: 12px;"> <%=i%></a>
			<%
				}
					}
					if (endPage < numOfPages) {
			%>
			<a href="?act=dv&catid=<%=sCatId%>&page=<%=endPage + 1%>"
				class="style_paging">Tiếp</a>
			<a href="?act=dv&catid=<%=sCatId%>&page=<%=numOfPages%>"
				class="style_paging">Cuối</a>
			<%
				}
			%>
		</div>

		<%
			}
		%>
	</div>

	<div class="box clearfix">
		<div class="title padbot5px">
			<h3>
				<img src="images/icon-game.png" width="23" height="23" alt=""
					align="left">
				<strong> Danh mục game</strong>
			</h3>
		</div>
		<!--end .title-->
		<ul class="list-dot">
			<%
				String catGameParentId = Config.getStringProperty(
						"CAT_GAME_PARENT_ID", "84", false);
				String catGameIdIgnore = Config.getStringProperty(
						"CAT_GAME_ID_IGNORE", "", false);
				if (!StringTool.isEmptyOrNul(catGameParentId)) {
					ArrayList<DatabaseObject> gameCategoryList = contentDAO
							.getCategory(catGameParentId, catGameIdIgnore);
					for (DatabaseObject game : gameCategoryList) {
			%>
			<li>
				<img src="images/icon-list.png"
					alt="<%=game.getAttribute().get("DESCRIPTION")%>">
				<a href="game.jsp?act=dv&catid=<%=game.getAttribute().get("ID")%>">
					<%=game.getAttribute().get("DESCRIPTION")%> </a>
				<%
					}
					}
				%>
			
		</ul>
	</div>
	<!-- END BODY CONTENT -->
</div>
<!--end .box bottom10-->
<%@include file="inc-nav-bottom.jsp"%>
<!-- END MAIN CONTENT -->
<%@include file="inc-footter.jsp"%>
